<?php
namespace app\admin\controller;
use think\facade\{View,Session,Db};
use app\BaseController;
use app\validate\Auser as veAuser;

use think\facade\Queue;
class Login extends BaseController
{
    public function login()
    {	
        if(Session('lt_auser')){
          return redirect('/index/index');
        }
       return View::fetch('login/login');
    }
 
    public function dologin(){
      $param=input("param.");
      if( !trim(input('username')) ){
        return returnmsg(201,"请输入账号"); 
      }
      if( !trim(input('password')) ){
        return returnmsg(201,"请输入密码"); 
      }
  
      if(!captcha_check(input('code'))){
        return returnmsg(201,"验证码错误"); 
      }

      $msg=Db::name("auser")->where("username",$param['username'])->find();

      if( !$msg ){ return returnmsg(201,"该账号不存在"); }
      
      if( md5($param['password']) != $msg['password'] ){ return returnmsg(201,"密码错误");  }
      
      Session("lt_auser",$msg);
      
      return returnmsg(200,"登录成功");
    }
    public function logout(){
        session::delete( "lt_auser" );
        return redirect('/admin/login/login');
    }

    public function test(){

    
      //开启任务
      $job="Job1";
      $data=["order_id"=>199,"order_status"=>"待发货",'time'=>date("Y-m-d H:i:s")];

      //$isPushed =Queue::push($job, $data);//调$job 方法  - 并在  redis中存入相关计划踪迹
      $isPushed =Queue::later(60, $job, $data);//一分钟后自动执行该方法  -- 创建与异步消费
      if ($isPushed) {
          echo '推送成功';
      } else {
          echo '推送失败';
      }


    }

     //发布消息
     public function publish()
     {
         $msg = input('msg');
         $server = '127.0.0.1';
         $port = 7788;
         $username = 'admin';
         $password = '123456';
         $clientId = 'test_emqx'. rand(1000000, 99999999);
 
         $mqtt = new \app\command\service\MqttNewService($server, $port, $clientId);
 
         if ($mqtt->connect(true, NULL, $username, $password)) {
          //pack('H*',$msg)
             $mqtt->publish('test-mqtt/1', $msg, 0, false);
             $mqtt->close();
         } else {
             echo "Time out!\n";
         }
     }

   
}